1768B - Quick Sort - CodeForces Solution


greedy math

Please click on ads to support us..

Python Code:


import math
from multiprocessing import current_process
import os
import random
import re
import copy
import pdb
import sys





        

        








if __name__ == '__main__':
    t = int(input())

    for _ in range(t):
        [n, k] = list(map(int, str(input()).split(' ')))
        
        p = list(map(int, str(input()).split(' ')))

        dict = {}

        for idx, ele in enumerate(p):
            dict[ele] = idx

        check = True
        broken = -1
        prev = dict[1]
        for i in range(2, n+1):
            loc = dict[i]
            if loc < prev:
                check = False
                broken = i
                break
            
            prev = loc

        if check == True:
            print(0)
        
        else:
            res = int((n-broken+1)/k)
            if (n-broken+1) % k != 0:
                res += 1
            print(res)







 
                
            

            

            

        

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(0)
#define endl '\n'
#define int long long
#define ar array<int, 2>
#define arr array<int, 3>
const int N = 2e5 + 5, M = 2 * N;
const int inf = 0x3f3f3f3f;
int mod = 998244353; //1e9+7;
int t, n, m, k;

signed main()
{
    ios;
#ifdef DEBUG
    freopen("../1.in", "r", stdin);
#endif
    // 这题是发现规律的。。
    // 就是猜 。
    // 我们猜测原数组的1后面的升序是不用变的。。
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        int a[n + 1];
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        int l = 1;
        for (int i = 1; i <= n; ++i)
            if (a[i] == l)
                l++;
        n -= l - 1;
        cout << (n + k - 1) / k << endl;
    }
};


Comments

Submit
0 Comments
More Questions

1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House